package com.cszy.yydqbfq.dialog

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hbcysd.qybfq.R

import com.cszy.yydqbfq.adapter.GeDanpopListAdapter
import com.cszy.yydqbfq.bean.Song

import com.cszy.yydqbfq.bean.SongListBean
import com.cszy.yydqbfq.common.PlaylistRepository
import com.hlyj.http.ad.NewGroMoreFeedAdManager

import com.hnyyac.ad.mds.utils.BaseAdConstants

/**
 * 添加歌单底部弹窗
 */
class BottomPopupWithGeDanRecycler(
    private  val mContext: Context,
    private val dataList: List<SongListBean>,
    private val song: Song // 要添加的歌曲
) {
     private var newGromoreFeedAd : NewGroMoreFeedAdManager? =null
    private val popupView = LayoutInflater.from(mContext).inflate(R.layout.pop_bottom, null)
    private val popupWindow = PopupWindow(
        popupView,
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    ).apply {
        isOutsideTouchable = true
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        animationStyle = R.style.PopupAnimation
    }

    fun show(anchorView: View) {
        val recyclerView = popupView.findViewById<RecyclerView>(R.id.recy_pop_list)
        val ll_empty=popupView.findViewById<LinearLayout>(R.id.ll_empty)
        popupView.findViewById<TextView>(R.id.tv_title).text = "歌单列表"
        val mAdapter = GeDanpopListAdapter().apply {
            submitList(dataList.toMutableList())

        }

        if (dataList.isEmpty()) {
            Log.e("qin","dataList.isEmpty()")
            ll_empty.visibility = View.VISIBLE

        } else {
            Log.e("qin","dataList.isNotEmpty()")
            ll_empty.visibility = View.GONE      // 隐藏空布局

        }
        recyclerView.layoutManager = LinearLayoutManager(anchorView.context)
        recyclerView.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val repository = PlaylistRepository()
             if ( repository.addSongToPlaylist(adapter.items[position].id, song)){
                 showSuccessToast()
             }else{
                 showErrorToast()
             }
        }

        var iv_xiala= popupView.findViewById<View>(R.id.iv_xiala)
        iv_xiala.setOnClickListener {
            dismiss()
        }
        //信息流
        var frame_gedan=popupView.findViewById<FrameLayout>(R.id.frame_gedan)
        newGromoreFeedAd = NewGroMoreFeedAdManager(mContext as Activity, BaseAdConstants.FEEDAD_Id, frame_gedan,46)
        newGromoreFeedAd?.loadAd()
        popupWindow.showAtLocation(anchorView, Gravity.BOTTOM, 0, 0)
    }

    fun dismiss() {
        popupWindow.dismiss()
    }

    fun addSong(id:Long,song: Song){
        // 在这里添加歌曲到歌单的逻辑
    }

    private fun BottomPopupWithGeDanRecycler.showSuccessToast() {
        Toast.makeText(mContext, "添加成功", Toast.LENGTH_SHORT).show()
        dismiss()
    }

    private fun BottomPopupWithGeDanRecycler.showErrorToast() {
        Toast.makeText(mContext, "已存在歌单中", Toast.LENGTH_SHORT).show()
        dismiss()
    }

}




